﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Global.asax.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Defines the MvcApplication type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC
{
    using System.Linq;
    using System.Web.Mvc;
    using System.Web.Routing;

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.ServiceRuntime;

    using Spenta.WindowsAzure.Diagnostics;

    /// <summary>
    /// Defines the MvcApplication type
    /// </summary>
    public class MvcApplication : System.Web.HttpApplication
    {
        /// <summary>
        /// Registers the routes.
        /// </summary>
        /// <param name="routes">The routes.</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                string.Empty, // URL with parameters
                new { controller = "Public", action = "Map" });

            routes.MapRoute(
                "Admin", // Route name
                "admin/{controller}/{action}/{id}", // URL with parameters
                new { controller = "Admin", action = "Index", id = UrlParameter.Optional });

            routes.MapRoute(
                "Signin", // Route name
                "Signin", // URL with parameters
                new { controller = "Home", action = "Signin" });
        }

        // ReSharper disable InconsistentNaming

        /// <summary>
        /// Application_s the start.
        /// </summary>
        protected void Application_Start()

        // ReSharper restore InconsistentNaming
        {
            if (RoleEnvironment.IsAvailable)
            {
                CloudStorageAccount.SetConfigurationSettingPublisher(
                    (configName, configSetter) => configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)));

                ConfigureTraceListener();

                RoleEnvironment.Changed += RoleEnvironmentChanged;
            }

            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }

        /// <summary>
        /// Configures the trace listener.
        /// </summary>
        private static void ConfigureTraceListener()
        {
            bool enableTraceListener;
            string enableTraceListenerSetting = RoleEnvironment.GetConfigurationSettingValue("EnableTableStorageTraceListener");
            if (bool.TryParse(enableTraceListenerSetting, out enableTraceListener))
            {
                if (enableTraceListener)
                {
                    var listener =
                        new TableStorageTraceListener("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString")
                        {
                            Name = "TableStorageTraceListener"
                        };
                    System.Diagnostics.Trace.Listeners.Add(listener);
                    System.Diagnostics.Trace.AutoFlush = true;
                }
                else
                {
                    System.Diagnostics.Trace.Listeners.Remove("TableStorageTraceListener");
                }
            }
        }

        /// <summary>
        /// Roles the environment changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironmentChangedEventArgs"/> instance containing the event data.</param>
        private static void RoleEnvironmentChanged(object sender, RoleEnvironmentChangedEventArgs e)
        {
            // configure trace listener for any changes to EnableTableStorageTraceListener 
            if (e.Changes.OfType<RoleEnvironmentConfigurationSettingChange>().Any(change => change.ConfigurationSettingName == "EnableTableStorageTraceListener"))
            {
                ConfigureTraceListener();
            }
        }
    }
}